package com.kyzh.core.activities

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.method.LinkMovementMethod
import com.blankj.utilcode.util.ActivityUtils
import com.kzd.gtgame.R
import com.kzd.gtgame.app.SPSConstance
import com.kzd.gtgame.base.BaseViewModel
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.custom.toAgreeLinks
import com.kzd.gtgame.main.*
import com.kzd.gtgame.main.access.AccessActivity
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_auth.*

class AuthActivity : OwnerViewModeActivity<BaseViewModel>() {

    private val mActivity: Activity get() = this

    override fun viewModel(): BaseViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_auth

    override fun initView() {
        tvAuthLogin.setOnClickAntiShakeListener {
            finishToResult(SPSConstance.uid)
        }
    }

    override fun initData() {
        tvAuthAgree.text = resources.getString(R.string.app_pact1).toAgreeLinks(activity = this)
        tvAuthAgree.movementMethod = LinkMovementMethod.getInstance()
    }

    override fun onResume() {
        super.onResume()
        if (SPSConstance.isReadAgree == false) {
            toAgreeActivity()
            return
        }
        if (checkLogin()) {
            return
        }
        val bundle = Bundle().apply {
            putSerializable(KEY_ACCESS_ACTION, AccessAction.LOGIN_AUTH)
        }
        ActivityUtils.startActivityForResult(
            bundle, mActivity, AccessActivity::class.java, AUTH_REQUEST_CODE
        )
    }

    private fun finishToResult(uid: String?) {
        val intent = Intent()
        intent.putExtra(AUTH_KEY_UID, uid)
        setResult(AUTH_RESULT_CODE, intent)
        finish()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == AUTH_REQUEST_CODE && resultCode == AUTH_RESULT_CODE) {
            finishToResult(data?.getStringExtra(AUTH_KEY_UID))
        } else {
            finish()
        }
    }
}